#include <stdio.h>
#include <console.h>
#include <config.h>
#include <errno.h>
#include <string.h>
#include <init.h>

int getchar(void)
{
	char c;
	int ret;

	ret = console_read(&c, 1);
	if (IS_ERR(ret))
		return ret;

	return c;
}

int putc(int c)
{
	char ch = c;

	return console_write(&ch, 1);
}

int puts(const char *s)
{
	return console_write(s, strlen(s));
}

int vprintf(const char *fmt, va_list args)
{
    int res;
    char printbuffer[CONFIG_SYS_PRINT_BUFFER_SIZE];

    res = vsnprintf(printbuffer, sizeof(printbuffer), fmt, args);

	console_write(printbuffer, res);
    return res;
}

int printf(const char *fmt, ...)
{
    va_list args;
	int res;

    va_start(args, fmt);
    res = vprintf(fmt, args);
    va_end(args);

    return res;
}

static int stdio_init(void)
{
	return console_open();	
}
late0_initcall(stdio_init);

static void stdio_exit(void)
{
	console_close();
}
exitcall0(stdio_exit);

